/*
 * Copyright (c) 2011, Nikolaus Moll
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * * Neither the name of the gradecalculator.net nor the
 *   names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package net.gradecalculator.gui.qt.editor.jowidgets;

import net.gradecalculator.core.interfaces.IRegulationsItem;
import net.miblounge.jombi.impl.widgets.internal.FrameWrapper;

import org.jowidgets.api.toolkit.Toolkit;
import org.jowidgets.api.validation.IValidator;
import org.jowidgets.api.validation.ValidationResult;
import org.jowidgets.api.widgets.IComposite;
import org.jowidgets.api.widgets.IInputComponent;
import org.jowidgets.api.widgets.IInputDialog;
import org.jowidgets.api.widgets.IValidationLabel;
import org.jowidgets.api.widgets.IWindow;
import org.jowidgets.api.widgets.blueprint.IInputDialogBluePrint;
import org.jowidgets.api.widgets.blueprint.IInputFieldBluePrint;
import org.jowidgets.api.widgets.blueprint.IScrollCompositeBluePrint;
import org.jowidgets.api.widgets.blueprint.ITextLabelBluePrint;
import org.jowidgets.api.widgets.blueprint.IValidationLabelBluePrint;
import org.jowidgets.api.widgets.blueprint.factory.IBluePrintFactory;
import org.jowidgets.api.widgets.content.IInputContentContainer;
import org.jowidgets.api.widgets.content.IInputContentCreator;
import org.jowidgets.common.widgets.layout.MigLayoutDescriptor;
import org.jowidgets.tools.powo.JoDialog;

public class JoModuleDialog<T extends IRegulationsItem>  {

	private final IInputDialog<T> inputDialog;
	private final String title;

	public JoModuleDialog(final FrameWrapper<?> container, final String title) {
		this.title = title;
		final IBluePrintFactory bpF = Toolkit.getBluePrintFactory();
		final IInputDialogBluePrint<T> inputDialogBp = bpF.inputDialog(new DemoInputDialogContent());
		inputDialogBp.setTitle(title);
		inputDialogBp.setValidationLabel(null);
//		inputDialogBp.setMissingInputText("Please fill out all mandatory (*) fields");
//		inputDialogBp.setMissingInputIcon(IconsSmall.INFO);
		inputDialog = container.createChildWindow(inputDialogBp);
	}

	public IInputDialog<T> getInputDialog() {
		return inputDialog;
	}
	
	public void apply()
	{
		//((DemoInputDialogContent)getInputDialog()).apply();
	}

	private class DemoInputDialogContent implements IInputContentCreator<T> {

		private IInputComponent<String> name;
		private T item;

		@Override
		public void createContent(final IInputContentContainer container) {
			final IBluePrintFactory bpF = Toolkit.getBluePrintFactory();
			container.setLayout(new MigLayoutDescriptor("[left, grow]", "[top, grow]"));
			
			final IScrollCompositeBluePrint scrollCompositeBp = bpF.scrollComposite(title);
			scrollCompositeBp.setMigLayout("[][grow, 250:250:800][260::]", "[]");
			final IComposite group = container.add(scrollCompositeBp, "grow");
			group.setLayout(new MigLayoutDescriptor("[][grow][20::]", "[]"));

			final String inputWidgetConstraints = "w 270:270:, grow, sg fg";

			final IValidator<String> maxLengthValidator = new IValidator<String>() {

				@Override
				public ValidationResult validate(final String validationInput) {
					final ValidationResult result = new ValidationResult();
					if (validationInput != null && validationInput.length() > 50) {
						result.addValidationError("Input must not have more than 50 characters");
					}
					return result;
				};

			};

			final ITextLabelBluePrint textLabelBp = bpF.textLabel().alignRight();

			final IValidationLabelBluePrint validationLabelBp = bpF.validationLabel().setShowValidationMessage(false);

			final IInputFieldBluePrint<String> stringMandatoryFieldBp = bpF.inputFieldString().setMandatory(true).setMaxLength(51);
			final IInputFieldBluePrint<String> stringFieldBp = bpF.inputFieldString().setMandatory(false).setMaxLength(51);
			stringMandatoryFieldBp.setValidator(maxLengthValidator);
			stringFieldBp.setValidator(maxLengthValidator);

//			container.add(textLabelBp.setText("Gender"), "right, sg lg");
//			gender = container.add(bpF.comboBoxSelection("Male", "Female", " ").setMandatory(false), inputWidgetConstraints);
//			final IValidationLabel genderValidationWidget = container.add(validationLabelBp, "wrap");
//			genderValidationWidget.registerInputWidget(gender);

			group.add(textLabelBp.setText("Name*"), "right, sg lg");
			name = group.add(stringMandatoryFieldBp, inputWidgetConstraints);
			final IValidationLabel nameValidationWidget = group.add(validationLabelBp, "wrap");
			nameValidationWidget.registerInputWidget(name);

//			container.add(textLabelBp.setText("Street*"), "right, sg lg");
//			street = container.add(stringMandatoryFieldBp, inputWidgetConstraints);
//			final IValidationLabel streetValidationWidget = container.add(validationLabelBp, "wrap");
//			streetValidationWidget.registerInputWidget(street);
//
//			container.add(textLabelBp.setText("Postal code*"), "right, sg lg");
//			postalCode = container.add(bpF.inputFieldIntegerNumber().setMaxLength(5).setMandatory(true), inputWidgetConstraints);
//			final IValidationLabel postalCodeValidationWidget = container.add(validationLabelBp, "wrap");
//			postalCodeValidationWidget.registerInputWidget(postalCode);
//
//			container.add(textLabelBp.setText("City*"), "right, sg lg");
//			city = container.add(stringMandatoryFieldBp, inputWidgetConstraints);
//			final IValidationLabel cityValidationWidget = container.add(validationLabelBp, "wrap");
//			cityValidationWidget.registerInputWidget(city);
//
//			container.add(textLabelBp.setText("Country*"), "right, sg lg");
//			final IComboBoxBluePrint<String> countryBp = bpF.comboBox("Germany", "Spain", "Italy", "United States");
//			countryBp.setMandatory(true).setValidator(maxLengthValidator);
//			country = container.add(countryBp, inputWidgetConstraints);
//			final IValidationLabel countryValidationWidget = container.add(validationLabelBp, "wrap");
//			countryValidationWidget.registerInputWidget(country);
//
//			container.add(textLabelBp.setText("Phone number"), "right, sg lg");
//			phoneNumber = container.add(stringFieldBp, inputWidgetConstraints);
//			final IValidationLabel phoneValidationWidget = container.add(validationLabelBp, "wrap");
//			phoneValidationWidget.registerInputWidget(phoneNumber);
//
//			container.add(textLabelBp.setText("Email"), "right, sg lg");
//			mail = container.add(stringFieldBp, inputWidgetConstraints);
//			mail.addValidator(moreThanOneWordValidator);
//			final IValidationLabel mailValidationWidget = container.add(validationLabelBp, "wrap");
//			mailValidationWidget.registerInputWidget(mail);
			
			container.registerInputWidget("Name", name);
		}

		@Override
		public void setValue(final T value) {
			item = value;

			name.setValue(item.getName());
		}

		@SuppressWarnings("unchecked")
		@Override
		public T getValue() {
			System.out.println("getValue()");
			return item;
		}
		
		public void apply()
		{
			item.setName(name.getValue());
		}

		@Override
		public ValidationResult validate() {
			return new ValidationResult();
		}

		@Override
		public boolean isMandatory() {
			return true;
		}
		
		

	}

}
